package cn.lixu.org.service.impl;

import cn.lixu.basic.query.BaseQuery;
import cn.lixu.basic.service.impl.BaseServiceImpl;
import cn.lixu.basic.util.PageList;
import cn.lixu.org.domain.Department;
import cn.lixu.org.mapper.DepartmentMapper;
import cn.lixu.org.query.DepartmentQuery;
import cn.lixu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    public void save(Department department) {
        //返回自增主键id
        departmentMapper.save(department);
        if (department.getParent_id() == null) {
            Long id = department.getId();
            department.setDirPath("/" + id);
        } else {
            //通过parent_id，获取上级的dirpath
            Long parent_id = department.getParent_id();
            //父级部门对象
            Department parentDapartment = departmentMapper.loadById(parent_id);
            //dirpath
            department.setDirPath(parentDapartment.getDirPath() + "/" + department.getId());

        }
        //调用update方法，更新数据库
        update(department);
    }


    @Override
    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    public void update(Department department) {
        if (department.getParent_id() == null) {
            Long id = department.getId();
            department.setDirPath("/" + id);
        } else {
            //通过parent_id，获取上级的dirpath
            Long parent_id = department.getParent_id();
            //父级部门对象
            Department parentDapartment = departmentMapper.loadById(parent_id);
            //dirpath
            department.setDirPath(parentDapartment.getDirPath() + "/" + department.getId());

        }
        departmentMapper.update(department);

    }


    //部门树
    @Override
    public List<Department> deptTree() {
        //获取全部部门信息
        List<Department> list = departmentMapper.loadAll();
        //准备一个map集合，用来装所有的部门
        HashMap<Long, Department> map = new HashMap<>();
        //遍历所有的部门信息
        for (Department department : list) {
            //将部门信息装入map集合中
            map.put(department.getId(), department);
        }

        List<Department> deptTree = new ArrayList<>();
        for (Department department : list) {
            if (department.getParent_id() == null) {//顶级部门
                deptTree.add(department);
            } else {
                //获取不是顶级部门的id
                Long parent_id = department.getParent_id();
                //通过id在map里面去获取响应的部门信息
                Department parentDepartment = map.get(parent_id);
                //将自己添加到上级部门里面
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }


}

